package com.sugar.follow.up.module.business.controller.admin.patient.fszl;

import com.sugar.follow.up.framework.security.core.util.SecurityFrameworkUtils;
import com.sugar.follow.up.module.business.controller.admin.patient.fszl.vo.PatientFszlPageReqVO;
import com.sugar.follow.up.module.business.controller.admin.patient.fszl.vo.PatientFszlRespVO;
import com.sugar.follow.up.module.business.controller.admin.patient.fszl.vo.PatientFszlSaveReqVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.follow.up.framework.common.pojo.PageParam;
import com.sugar.follow.up.framework.common.pojo.PageResult;
import com.sugar.follow.up.framework.common.pojo.CommonResult;
import com.sugar.follow.up.framework.common.util.object.BeanUtils;
import static com.sugar.follow.up.framework.common.pojo.CommonResult.success;

import com.sugar.follow.up.framework.excel.core.util.ExcelUtils;

import com.sugar.follow.up.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.follow.up.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.follow.up.module.business.dal.dataobject.patientfszl.PatientFszlDO;
import com.sugar.follow.up.module.business.service.patientfszl.PatientFszlService;

@Tag(name = "管理后台 - 放射治疗")
@RestController
@RequestMapping("/business/patient-fszl")
@Validated
public class PatientFszlController {

    @Resource
    private PatientFszlService patientFszlService;

    @PostMapping("/create")
    @Operation(summary = "创建放射治疗")
    //@PreAuthorize("@ss.hasPermission('business:patient-fszl:create')")
    public CommonResult<Long> createPatientFszl(@Valid @RequestBody PatientFszlSaveReqVO createReqVO) {
        return success(patientFszlService.createPatientFszl(createReqVO, SecurityFrameworkUtils.getLoginUserId()));
    }

    @PutMapping("/update")
    @Operation(summary = "更新放射治疗")
    //@PreAuthorize("@ss.hasPermission('business:patient-fszl:update')")
    public CommonResult<Boolean> updatePatientFszl(@Valid @RequestBody PatientFszlSaveReqVO updateReqVO) {
        patientFszlService.updatePatientFszl(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除放射治疗")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('business:patient-fszl:delete')")
    public CommonResult<Boolean> deletePatientFszl(@RequestParam("id") Long id) {
        patientFszlService.deletePatientFszl(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除放射治疗")
                //@PreAuthorize("@ss.hasPermission('business:patient-fszl:delete')")
    public CommonResult<Boolean> deletePatientFszlList(@RequestParam("ids") List<Long> ids) {
        patientFszlService.deletePatientFszlListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得放射治疗")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('business:patient-fszl:query')")
    public CommonResult<PatientFszlRespVO> getPatientFszl(@RequestParam("id") Long id) {
        PatientFszlDO patientFszl = patientFszlService.getPatientFszl(id);
        return success(BeanUtils.toBean(patientFszl, PatientFszlRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得放射治疗分页")
    //@PreAuthorize("@ss.hasPermission('business:patient-fszl:query')")
    public CommonResult<PageResult<PatientFszlRespVO>> getPatientFszlPage(@Valid PatientFszlPageReqVO pageReqVO) {
        PageResult<PatientFszlDO> pageResult = patientFszlService.getPatientFszlPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PatientFszlRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出放射治疗 Excel")
    //@PreAuthorize("@ss.hasPermission('business:patient-fszl:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPatientFszlExcel(@Valid PatientFszlPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PatientFszlDO> list = patientFszlService.getPatientFszlPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "放射治疗.xls", "数据", PatientFszlRespVO.class,
                        BeanUtils.toBean(list, PatientFszlRespVO.class));
    }

}